﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Management;

namespace MSL.Utility.Device
{
    /// <summary>
    /// 硬件信息类
    /// </summary>
    internal class Hardware
    {
        #region CPU信息

        /// <summary>
        /// 获得cpu名
        /// </summary>
        internal static string CPUName
        {
            get
            {
                string cpuinfo = "unknow";
                ManagementClass mc = new ManagementClass("win32_processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuinfo = mo["Name"].ToString();
                }
                moc.Dispose();
                return cpuinfo;
            }
        }
        /// <summary>
        /// 获得cpu sn编号 
        /// </summary>
        private static string CPUSN
        {
            get
            {
                string sn = "";//cpu序列号
                ManagementClass cimobject = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = cimobject.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    sn = mo.Properties["ProcessorId"].Value.ToString();
                }
                return sn;
            }
        }
        #endregion

        #region 硬盘信息
        
        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern int GetVolumeInformation(string lpRootPathName, string lpVolumeNameBuffer, int nVolumeNameSize, ref int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);
        /// <summary>
        /// 硬盘id
        /// </summary>
        private static string HardDiskID
        {
            get
            {
                string hdid = "";
                ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc1 = cimobject1.GetInstances();
                foreach (ManagementObject mo in moc1)
                {
                    hdid = mo.Properties["Model"].Value.ToString();
                }
                return hdid;
            }
        }

        /// <summary>
        /// 获得盘符为drvID的硬盘序列号，缺省为C
        /// </summary>
        /// <param name="drvID"></param>
        /// <returns></returns>
        public static string HardDiskVal(string drvID)
        {
            const int MAX_FILENAME_LEN = 256;
            int retVal = 0;
            int a = 0;
            int b = 0;
            string str1 = null;
            string str2 = null;

            int i = GetVolumeInformation(
                drvID + @":\",
                str1,
                MAX_FILENAME_LEN,
                ref retVal,
                a,
                b,
                str2,
                MAX_FILENAME_LEN
                );

            return retVal.ToString();
        }

        public static string HardDiskVal()
        {
            return HardDiskVal("C");
        }
        #endregion

        #region PC NAME

        /// <summary>
        /// 获取计算机名
        /// </summary>
        /// <returns></returns>
        public static string GetComputerName()
        {
            return System.Environment.MachineName;
        }
        #endregion

        #region 系统登录名

        /// <summary>
        /// 获取操作系统的登录用户名
        /// </summary>
        /// <returns></returns>
        public static string LoginName()
        {
            return Environment.UserName;
        }
        #endregion

        #region MAC地址

        /// <summary>
        /// mac地址
        /// </summary>
        internal static string MAC
        {
            get
            {
                string mac = "unknow";
                ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mAdapter.GetInstances();
                foreach (ManagementBaseObject mo in moc)
                {
                    if ((bool)mo["IpEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                    }
                }
                moc.Dispose();
                return mac;
            }

        }
        #endregion

        #region 内存

        /// <summary>
        /// 内存大小
        /// </summary>
        internal static string MemorySize
        {
            get
            {
                ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                double sizeAll = 0.0;
                foreach (ManagementObject mo in moc)
                {
                    if (mo.Properties["TotalVisibleMemorySize"].Value != null)
                    {
                        sizeAll += System.Convert.ToDouble(mo.Properties["TotalVisibleMemorySize"].Value.ToString());
                    }
                }
                //sizeAll /= 1000000;
                moc.Dispose();
                return sizeAll.ToString("0.###");
            }
        }
        #endregion

        #region IP

        /// <summary>
        /// ip地址
        /// </summary>
        internal static string[] IPAddress
        {
            get
            {
                string hostname = System.Net.Dns.GetHostName();
                System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostname);
                string[] ips = new string[ipEntry.AddressList.Length];
                for (int i = 0; i < ipEntry.AddressList.Length; i++)
                {
                    ips[i] = ipEntry.AddressList[i].ToString();
                }
                return ips;
            }
        }
        #endregion

        #region USB盘符列表

        /// <summary>
        /// Returns a List of drive letters of USB storage devices attached to the computer.
        /// Drive letter format is of the format X:
        /// </summary>
        /// <returns>List of USB Drive letters</returns>
        public static List<string> GetUSBDriveLetters()
        {
            List<string> list = new List<string>();
            ManagementObjectSearcher ddMgmtObjSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

            foreach (ManagementObject ddObj in ddMgmtObjSearcher.Get())
            {
                foreach (ManagementObject dpObj in ddObj.GetRelated("Win32_DiskPartition"))
                {
                    foreach (ManagementObject ldObj in dpObj.GetRelated("Win32_LogicalDisk"))
                    {
                        list.Add(ldObj["DeviceID"].ToString());
                    }
                }
            }

            return list;
        }
        #endregion

        #region 系统版本

        /// <summary>
        /// 系统版本
        /// </summary>
        public static string OSVersion
        {
            get
            {
                return System.Environment.OSVersion.VersionString;
            }

        }

        /// <summary>
        /// 获取PC类型
        /// </summary>
        /// <returns></returns>
        public static string SystemType()
        {
            string st = "";
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {

                st = mo["SystemType"].ToString();

            }
            return st;
        }
        #endregion
    }
}
